%pylab inline
Populating the interactive namespace from numpy and matplotlib
!wget http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.quakeml
--2016-03-29 12:51:49-- http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.quakeml Resolving earthquake.usgs.gov (earthquake.usgs.gov)... 173.223.52.217, 173.223.52.216, 2607:f380:1:6001::cc66:7229, ... Connecting to earthquake.usgs.gov (earthquake.usgs.gov)|173.223.52.217|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 9270 (9,1K) [application/xml] Saving to: ‘all_hour.quakeml.2’ all_hour.quakeml.2 100%[=====================>] 9,05K --.-KB/s in 0s 2016-03-29 12:51:49 (319 MB/s) - ‘all_hour.quakeml.2’ saved [9270/9270]
import xml.etree.ElementTree as ET
tree = ET.parse('all_hour.quakeml')
root = tree.getroot()
Good tutorial on ElementTree: http://getpython3.com/diveintopython3/xml.html#xml-parse
Other options:
root
<Element '{http://quakeml.org/xmlns/quakeml/1.2}quakeml' at 0x7f60959d70d0>
root.tag
'{http://quakeml.org/xmlns/quakeml/1.2}quakeml'
root.attrib
{}
for child in root:
print child.tag, ":", child.attrib
{http://quakeml.org/xmlns/bed/1.2}eventParameters : {'publicID': 'quakeml:earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.quakeml#requested=2014-03-25T20:27:27.000Z'}
root[0]
<Element '{http://quakeml.org/xmlns/bed/1.2}eventParameters' at 0x7f60959d7110>
eventParameters = root[0]
eventParameters.getchildren()
[<Element '{http://quakeml.org/xmlns/bed/1.2}event' at 0x7f60959d7150>, <Element '{http://quakeml.org/xmlns/bed/1.2}event' at 0x7f60959d7c50>, <Element '{http://quakeml.org/xmlns/bed/1.2}event' at 0x7f6095b2a7d0>, <Element '{http://quakeml.org/xmlns/bed/1.2}event' at 0x7f6095b0b350>, <Element '{http://quakeml.org/xmlns/bed/1.2}event' at 0x7f6095b0bd90>, <Element '{http://quakeml.org/xmlns/bed/1.2}event' at 0x7f6095c10810>, <Element '{http://quakeml.org/xmlns/bed/1.2}event' at 0x7f6095c15290>, <Element '{http://quakeml.org/xmlns/bed/1.2}creationInfo' at 0x7f6095c15e10>]
for child in eventParameters:
print child.tag, ":", child.attrib
{http://quakeml.org/xmlns/bed/1.2}event : {'{http://anss.org/xmlns/catalog/0.1}eventid': '72188865', 'publicID': 'quakeml:earthquake.usgs.gov/earthquakes/feed/v1.0/detail/nc72188865.quakeml', '{http://anss.org/xmlns/catalog/0.1}eventsource': 'nc', '{http://anss.org/xmlns/catalog/0.1}datasource': 'nc'} {http://quakeml.org/xmlns/bed/1.2}event : {'{http://anss.org/xmlns/catalog/0.1}eventid': '72188860', 'publicID': 'quakeml:earthquake.usgs.gov/earthquakes/feed/v1.0/detail/nc72188860.quakeml', '{http://anss.org/xmlns/catalog/0.1}eventsource': 'nc', '{http://anss.org/xmlns/catalog/0.1}datasource': 'nc'} {http://quakeml.org/xmlns/bed/1.2}event : {'{http://anss.org/xmlns/catalog/0.1}eventid': '72188855', 'publicID': 'quakeml:earthquake.usgs.gov/earthquakes/feed/v1.0/detail/nc72188855.quakeml', '{http://anss.org/xmlns/catalog/0.1}eventsource': 'nc', '{http://anss.org/xmlns/catalog/0.1}datasource': 'nc'} {http://quakeml.org/xmlns/bed/1.2}event : {'{http://anss.org/xmlns/catalog/0.1}eventid': '11198395', 'publicID': 'quakeml:earthquake.usgs.gov/earthquakes/feed/v1.0/detail/ak11198395.quakeml', '{http://anss.org/xmlns/catalog/0.1}eventsource': 'ak', '{http://anss.org/xmlns/catalog/0.1}datasource': 'ak'} {http://quakeml.org/xmlns/bed/1.2}event : {'{http://anss.org/xmlns/catalog/0.1}eventid': 'c000nq6f', 'publicID': 'quakeml:earthquake.usgs.gov/earthquakes/feed/v1.0/detail/usc000nq6f.quakeml', '{http://anss.org/xmlns/catalog/0.1}eventsource': 'us', '{http://anss.org/xmlns/catalog/0.1}datasource': 'us'} {http://quakeml.org/xmlns/bed/1.2}event : {'{http://anss.org/xmlns/catalog/0.1}eventid': '11198336', 'publicID': 'quakeml:earthquake.usgs.gov/earthquakes/feed/v1.0/detail/ak11198336.quakeml', '{http://anss.org/xmlns/catalog/0.1}eventsource': 'ak', '{http://anss.org/xmlns/catalog/0.1}datasource': 'ak'} {http://quakeml.org/xmlns/bed/1.2}event : {'{http://anss.org/xmlns/catalog/0.1}eventid': '15480193', 'publicID': 'quakeml:earthquake.usgs.gov/earthquakes/feed/v1.0/detail/ci15480193.quakeml', '{http://anss.org/xmlns/catalog/0.1}eventsource': 'ci', '{http://anss.org/xmlns/catalog/0.1}datasource': 'ci'} {http://quakeml.org/xmlns/bed/1.2}creationInfo : {}
eventParameters.findall("{http://quakeml.org/xmlns/bed/1.2}creationInfo")
[<Element '{http://quakeml.org/xmlns/bed/1.2}creationInfo' at 0x7f6095c15e10>]
creationInfo = eventParameters.findall("{http://quakeml.org/xmlns/bed/1.2}creationInfo")
creationInfo[0]
<Element '{http://quakeml.org/xmlns/bed/1.2}creationInfo' at 0x7f6095c15e10>
for child in creationInfo[0]:
print child
<Element '{http://quakeml.org/xmlns/bed/1.2}creationTime' at 0x7f6095c15e50>
creationTime = creationInfo[0].findall('{http://quakeml.org/xmlns/bed/1.2}creationTime')
creationTime
[<Element '{http://quakeml.org/xmlns/bed/1.2}creationTime' at 0x7f6095c15e50>]
creationTime[0].text
'2014-03-25T20:27:27.000Z'
events = eventParameters.findall('{http://quakeml.org/xmlns/bed/1.2}event')
events
[<Element '{http://quakeml.org/xmlns/bed/1.2}event' at 0x7f60959d7150>, <Element '{http://quakeml.org/xmlns/bed/1.2}event' at 0x7f60959d7c50>, <Element '{http://quakeml.org/xmlns/bed/1.2}event' at 0x7f6095b2a7d0>, <Element '{http://quakeml.org/xmlns/bed/1.2}event' at 0x7f6095b0b350>, <Element '{http://quakeml.org/xmlns/bed/1.2}event' at 0x7f6095b0bd90>, <Element '{http://quakeml.org/xmlns/bed/1.2}event' at 0x7f6095c10810>, <Element '{http://quakeml.org/xmlns/bed/1.2}event' at 0x7f6095c15290>]
events
[<Element '{http://quakeml.org/xmlns/bed/1.2}event' at 0x7f60959d7150>, <Element '{http://quakeml.org/xmlns/bed/1.2}event' at 0x7f60959d7c50>, <Element '{http://quakeml.org/xmlns/bed/1.2}event' at 0x7f6095b2a7d0>, <Element '{http://quakeml.org/xmlns/bed/1.2}event' at 0x7f6095b0b350>, <Element '{http://quakeml.org/xmlns/bed/1.2}event' at 0x7f6095b0bd90>, <Element '{http://quakeml.org/xmlns/bed/1.2}event' at 0x7f6095c10810>, <Element '{http://quakeml.org/xmlns/bed/1.2}event' at 0x7f6095c15290>]
for e in events[0]:
print e
<Element '{http://quakeml.org/xmlns/bed/1.2}origin' at 0x7f60959d7190> <Element '{http://quakeml.org/xmlns/bed/1.2}magnitude' at 0x7f60959d77d0> <Element '{http://quakeml.org/xmlns/bed/1.2}preferredOriginID' at 0x7f60959d7a50> <Element '{http://quakeml.org/xmlns/bed/1.2}preferredMagnitudeID' at 0x7f60959d7a90> <Element '{http://quakeml.org/xmlns/bed/1.2}type' at 0x7f60959d7ad0> <Element '{http://quakeml.org/xmlns/bed/1.2}creationInfo' at 0x7f60959d7b10>
By: Andrés Cabrera mantaraya36@gmail.com
For Course MAT 240F at UCSB
This ipython notebook is licensed under the CC-BY-NC-SA license: http://creativecommons.org/licenses/by-nc-sa/4.0/